package com.hlyj.http.base.tool.lib_hlyj_base


import android.app.Activity
import android.util.Log
import com.bytedance.sdk.openadsdk.TTFeedAd
import com.cszy.mfjx.lib_hlyj_base.BuildConfig
import com.hlyj.http.base.tool.lib_hlyj_base.listener.IChannelProvider
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.JsonUtil
import com.hnyyac.ad.mds.GMAds
import com.hnyyac.ad.mds.IAdType
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.ben.Cfs
import com.hnyyac.ad.mds.cache.CacheHelper
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.hnyyac.ad.mds.views_notload.IAdviewNotLoadFeedListener
import com.ok.common.api.GlobalApi
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure

object ProductEnvConstance {
    fun initsdk(activity: Activity, callback: BaseApplication.SDKInitCallback? = null) {
        GMAds.init(
            activity,
             Cfs(BaseAppConstans.DEBUG, BaseAdConstants.csjAppId),
            object : GMAds.InitCallback { // 添加GMAds的初始化回调
                override fun onSuccess() {
                     Log.e("initsdk", "SDK初始化onSuccess")
                    callback?.onInitSuccess()
                }
                override fun onFailed(code: Int, msg: String) {
                    Log.e("initsdk", "SDK初始化onFailed")
                    callback?.onInitFailed(code, msg)
                    val logContent = listOf(
                        mapOf(
                            Constants.TYPE to Constants.SDK_INIT_FAIL,
                            Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                            Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                            Constants.TIME to System.currentTimeMillis().toString(),
                            Constants.DATA to msg
                        )

                    )
                    GlobalApi.sendLog(logContent)
                }
            }
        )
        //插屏和激励视频的统一监听
        GMAds.setupLoadListener(object : IAdViewListener {
            override fun onTrigger(adView: IAdView) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.TRIGGER,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                //  Log.e("ProductEnvConstance", "onTrigger${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdShow(adView: IAdView) {
             Log.e("ProductEnvConstance", "adView${adView.getEcpm().toString()}")
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.SHOW,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.ADSOURCEID to adView.getSlotId(),
                        Constants.OCCODE to adView.getPosition(),
                        Constants.SHOWID to adView.getshowid_bg(),
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to adView.getEcpm(),
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
                // 用完补充
//                when(adView.getAdType()){
//                    IAdType.interstitial -> {
//                        CacheHelper.fillOneChaping(adView.getAdShowId(), adView.getPosition() ?: -1)
//                    }
//                    IAdType.reward_video -> {
//                        CacheHelper.fillOneVideo(adView.getAdShowId(), adView.getPosition() ?: -1)
//                    }
//                    else -> {
//                    }
//                }
            }
            override fun onAdClick(adView: IAdView) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLICK,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.ADSOURCEID to adView.getSlotId(),
                   //     Constants.OCCODE to ocCodee,
                        Constants.SHOWID to adView.getshowid_bg(),
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to adView.getEcpm(),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                // Log.e("ProductEnvConstance", "onAdClick${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClose(adView: IAdView) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLOSE,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.ADSOURCEID to adView.getSlotId(),
                     //   Constants.OCCODE to ocCodee,
                        Constants.SHOWID to adView.getshowid_bg(),
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to adView.getEcpm(),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                GlobalApi.sendLog(logContent)

            }

            override fun onError(msg: String) {
                Log.e("ProductEnvConstance", "onError")
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.FAIL,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        // Constants.OCCODE to "自定义广告位",
                        Constants.DATA to JsonUtil.parseStringToJsonWithMsg(msg),//这里
                        Constants.TIME to System.currentTimeMillis().toString()
                    )


                )
                // Log.e("ProductEnvConstance", "onAdClose${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onLoadError(adView: IAdView) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.FAIL,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        // Constants.OCCODE to "自定义广告位",
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.DATA to "onLoadError,广告加载失败",
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                // Log.e("ProductEnvConstance", "onAdClose${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onSuccess(adView: IAdView) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.REWARD_ARRIVED,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.OCCODE to ocCodee,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        // Constants.DATA to "onLoadError,广告加载完成但未就绪",
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                //Log.e("ProductEnvConstance", "onAdClose${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onLoad(adView: IAdView) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.LOAD,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to adView.getAdType(),
                        Constants.PLACEMENTID to adView.getAdShowId(),
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }


            override fun onSkipped() {
                
            }

            override fun onNoAd() {
                
            }

            override fun onTimeOut() {
                
            }

        })
        // 信息流的监听器
        GMAds.setupFeedListener(object : IAdviewNotLoadFeedListener {
            override fun onError(code: String,adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.FAIL,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to "native",
                        Constants.PLACEMENTID to adUnitId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.DATA to JsonUtil.parseStringToJsonWithMsg(code),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                GlobalApi.sendLog(logContent)
            }

            override fun onSuccess() {
                Log.e("ProductEnvConstance", "onSuccess")
            }

            override fun onAdClick(feedAd: TTFeedAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLICK,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to "native",
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to feedAd.mediationManager.showEcpm.slotId,
                      //  Constants.OCCODE to ocCodee,
                        Constants.SHOWID to feedAd.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to feedAd.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                Log.e("ProductEnvConstance", "onAdClick${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClose(feedAd: TTFeedAd, adUnitId: String) {
                Log.e("ProductEnvConstance", "onAdClose")
            }

            override fun onLoad(adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.LOAD,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to "native",
                        Constants.PLACEMENTID to adUnitId,
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                Log.e("ProductEnvConstance", "onLoad${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onTrigger(adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.TRIGGER,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to "native",
                        Constants.PLACEMENTID to adUnitId,
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                Log.e("ProductEnvConstance", "onTrigger${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdShow(feedAd: TTFeedAd, adUnitId: String,position: Int) {
                Log.d("ProductEnvConstance", "广告展示，广告单元 ID: $adUnitId"+feedAd.mediationManager.showEcpm.ecpm+"广告位ID"+feedAd.mediationManager.showEcpm.slotId+"广告位渠道"+feedAd.mediationManager.showEcpm.channel+"广告位类型"+feedAd.mediationManager.showEcpm.ritType)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.SHOW,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to "native",
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to feedAd.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to position,
                        Constants.SHOWID to feedAd.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to feedAd.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                    )

                )
                Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }
        })




        //   }

//        //Bugly上报
//        val strategy = UserStrategy(context)
//        strategy.setDeviceModel(DeviceModeUtil.getInstance().deviceModel)
//        CrashReport.initCrashReport(context, BuildConfig.buglyAppId, true, strategy)
//
        UMConfigure.setLogEnabled(true) //开启日志上线过后关闭
        UMConfigure.init(
            BaseApplication.application,
            BuildConfig.umengId,
            (BaseApplication.instance as IChannelProvider).getChannelId(),
            UMConfigure.DEVICE_TYPE_PHONE,
            null
        )
//        //友盟统计SDK
        UMConfigure.preInit(activity, BuildConfig.umengId,
            (BaseApplication.instance as IChannelProvider).getChannelId())
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
//        if (MyApplication.getInstance().isShowTime()) {
//            if (Build.VERSION.SDK_INT >= 25) {
//                val shortcutManager = context.getSystemService(ShortcutManager::class.java)
//                val intent_theme = Intent(context, FullScreenActivity::class.java)
//                intent_theme.setAction(Intent.ACTION_VIEW)
//                intent_theme.putExtra("islocal", true)
//                val shortcut_theme = ShortcutInfo.Builder(context, "local_theme")
//                    .setShortLabel(context.getString(R.string.uninstall))
//                    .setLongLabel(context.getString(R.string.uninstall))
//                    .setIcon(Icon.createWithResource(context, R.drawable.uninstall))
//                    .setIntent(intent_theme)
//                    .build()
//                shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut_theme))
//            }
//        }
    }


}